import axios from 'axios'
const baseURL = 'http://localhost:8888/api/private/v1/'
axios.defaults.baseURL = baseURL
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 将token给到一个前后台约定好的key中，作为请求发送
  var token = localStorage.getItem('mytoken')
  if (token) {
    // 写入请求头
    config.headers['Authorization'] = token
  }
  return config
}, function (error) {
  // Do something with request error
  return Promise.reject(error)
})
// 登录
export const login = (params) => {
  return axios.post('login', params).then((res) => {
    return res
  })
}
// 获取用户数据列表
export const getAllUserList = (params) => {
  return axios.get('users', {params: params}).then((res) => {
    // console.log(res)
    return res.data
  })
}
// 添加用户功能实现
export const addUser = (params) => {
  return axios.post('users', params).then((res) => {
    // console.log(res)
    return res.data
  })
}
// 编辑用户功能实现
export const editUser = (params) => {
  return axios.put(`users/${params.id}`, params).then((res) => {
    // console.log(res)
    return res.data
  })
}
// 删除用户功能实现
export const delectUser = (id) => {
  return axios.delete(`users/${id}`).then((res) => {
    // console.log(res)
    return res.data
  })
}
// 修改用户状态功能实现
export const uploadUserStatus = (params) => {
  return axios.put(`users/${params.id}/state/${params.status}`).then((res) => {
    // console.log(res)
    return res.data
  })
}
// 获取角色列表信息
export const getRolesList = () => {
  return axios.get('roles').then((res) => {
    // console.log(res)
    return res.data
  })
}

// 授权角色功能实现
export const putAccreditUser = (params) => {
  return axios.put(`users/${params.id}/role`, {rid: params.rid}).then((res) => {
    // console.log(res)
    return res.data
  })
}

// 获取权限列表信息
export const getPowerList = (type) => {
  return axios.get(`rights/${type}`).then((res) => {
    // console.log(res)
    return res.data
  })
}

// 删除角色权限
export const deleteRoleRight = (roleId, rightId) => {
  return axios.delete(`roles/${roleId.id}/rights/${rightId}`).then(res => {
    return res.data
  })
}

// 为角色指定授权权限
export const accreditRoler = (params) => {
  return axios.post(`roles/${params.roleid}/rights`, {rids: params.rids}).then(res => {
    return res.data
  })
}

// 添加角色功能
export const addRoler = (params) => {
  return axios.post('roles', params).then((res) => {
    // console.log(res)
    return res.data
  })
}

// 获取左侧菜单列表
export const getLeftMenuList = (type) => {
  return axios.get(`menus`).then((res) => {
    // console.log(res)
    return res.data
  })
}

// 获取商品数据列表
export const getCategoriesList = (type) => {
  return axios.get(`categories`, {params: {'type': type}}).then((res) => {
    // console.log(res)
    return res.data
  })
}
